package com.lx.core.weight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lx.core.util.DateUtils;
import com.lx.core.weight.entity.GoodsType;
import com.lx.core.weight.entity.dto.GoodsTypeDTO;
import com.lx.core.weight.entity.vo.GoodsTypePageList;
import com.lx.core.weight.entity.vo.GoodsTypeVO;
import com.lx.core.weight.entity.vo.WeightUpdatePageList;
import com.lx.core.weight.entity.vo.WeightUpdateVO;
import com.lx.core.weight.mapper.GoodsTypeMapper;
import com.lx.core.weight.service.GoodsTypeService;
import com.lx.framework.common.R;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author lx
 * @date 2025/08/26 12:20
 **/
@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsType> implements GoodsTypeService {
    private final GoodsTypeMapper goodsTypeMapper;

    public GoodsTypeServiceImpl(GoodsTypeMapper goodsTypeMapper) {
        this.goodsTypeMapper = goodsTypeMapper;
    }

    @Override
    public List<GoodsType> getGoodsTypeNameList() {
        return this.list(null);
    }

    @Override
    public GoodsTypeVO findGoodsTypePage(IPage<GoodsTypeVO> page, GoodsTypeDTO goodsTypeDTO) {
        IPage<GoodsTypePageList> pageList = goodsTypeMapper.findGoodsTypePage(page, goodsTypeDTO);
        // 构建返回结果
        GoodsTypeVO result = new GoodsTypeVO();
        result.setRecords(pageList.getRecords());
        result.setTotal(pageList.getTotal());
        result.setSize(pageList.getSize());
        result.setCurrent(pageList.getCurrent());
        result.setPages(pageList.getPages());
        return result;
    }

    @Override
    public boolean deleteGoodsType(int id) {
        int result = goodsTypeMapper.deleteById(id);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public R addGoodsType(String goodsTypeName, boolean prioritySelect) {
        QueryWrapper query = new QueryWrapper();
        query.eq("name", goodsTypeName);
        List<GoodsType> queryList = goodsTypeMapper.selectList(query);
        if (queryList.size() > 0) {
            return R.error("货物类型已存在");
        }

        int result = goodsTypeMapper.insert(new GoodsType(goodsTypeName, new Date(), prioritySelect));
        if (result > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    @Override
    public R editGoodsType(int id, boolean prioritySelect) {
        GoodsType goodsType = goodsTypeMapper.selectById(id);
        goodsType.setPrioritySelect(prioritySelect);
        int result = goodsTypeMapper.updateById(goodsType);
        if (result > 0) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }
}
